0% completed
Problem Statement
Given an integer array nums containing n elements and an integer k, return the number of pairs (i, j) such that 0 <= i < j <= n - 1 and nums[i] * nums[j] is divisible by k.
Examples
-
Example 1:
- Input:
nums = [8, 4, 6, 12],k = 4 - Expected Output:
6 - Justification: The pairs that satisfy the conditions are (8, 4), (8, 6), (8, 12), (4, 6), (4, 12), and (6, 12). The products of these pairs are divisible by 4.
- Input:
-
Example 2:
- Input:
nums = [10, 5, 2, 6],k = 5 - Expected Output:
5 - Justification: The valid pairs are (10, 5), (10, 2), (5, 2), (5, 6), and (10, 6). Each product of these pairs is divisible by 5.
- Input:
-
Example 3:
- Input:
nums = [4, 7, 9, 11, 23],k = 3 - Expected Output:
4 - Justification: The pairs are (4, 9), (7, 9), (9, 11), and (9, 23). The products of these pairs are divisible by 3.
- Input:
Solution
To solve this problem, we adopt a mathematical and hash map-based approach that focuses on the properties of divisibility and the Greatest Common Divisor (GCD). The core idea hinges on the fact that if the product of two numbers is divisible by k, then the GCD of each number with k must also meet certain divisibility conditions.
Specifically, for two numbers a and b, if gcd(a, k) * gcd(b, k) is divisible by k, then a * b is divisible by k. This insight allows us to transform the problem into one of counting pairs based on their GCDs with k, significantly reducing the problem's complexity from potentially quadratic to more manageable levels. We leverage a hash map to efficiently count and pair up elements with compatible GCDs, ensuring a thorough yet efficient examination of all possible pairings without direct multiplication, thus optimizing both performance and scalability.
Step-by-Step Algorithm
-
Initialize a result variable: This variable will hold the final count of pairs whose product is divisible by
k. -
Create a hash map (
gcdMap): This map will store the greatest common divisor (GCD) of the elements in the array withkas keys, and the frequency of these GCDs as values. -
Iterate through the array:
- For each element in the array, calculate the GCD of the element and
k. - For every GCD value calculated, iterate through the keys in
gcdMap:- If the product of the current GCD and any GCD in the map is divisible by
k(checking(long) gcd * num % k == 0to avoid integer overflow), add the frequency of that GCD in the map to the result. This step effectively counts pairs formed by the current element and elements previously processed.
- If the product of the current GCD and any GCD in the map is divisible by
- Update
gcdMapwith the current GCD, incrementing its frequency by 1. If the GCD is already a key in the map, its value is increased; otherwise, it is added to the map with a frequency of 1.
- For each element in the array, calculate the GCD of the element and
-
Return the result: After iterating through all elements in the array and updating the result based on the conditions checked, return the final count of pairs.
Algorithm Walkthrough
Let's consider the Input: nums= [10, 5, 2, 6], k = 5.
-
Initialization: Start with an empty
gcdMapand result set to 0. -
First element (10):
- Compute
gcd(10, 5) = 5. - No existing keys in
gcdMap, so nothing to add to the result yet.result = 0. - Update
gcdMapto{5: 1}.
- Compute
-
Second element (5):
- Compute
gcd(5, 5) = 5. - Existing key
5ingcdMapwith frequency 1. Since5 * 5 % 5 == 0, add 1 to the result.result = 1. - Update
gcdMapto{5: 2}(since5appears twice now).
- Compute
-
Third element (2):
- Compute
gcd(2, 5) = 1. - For existing key
5ingcdMap:5 * 1 % 5 == 0, add 2 to the result (because there are 2 elements with GCD 5 so far).result = 3. - Update
gcdMapto{5: 2, 1: 1}.
- Compute
-
Fourth element (6):
- Compute
gcd(6, 5) = 1. - For existing key
5ingcdMap:5 * 1 % 5 == 0, add 2 to the result.result = 5. - For existing key
1ingcdMap:1 * 1 % 5 == 4. So, don't need to add anything in result. - Update
gcdMapto{5: 2, 1: 2}.
- Compute
-
Final result: The total count of valid pairs is 5.
Code
Complexity Analysis
-
Time Complexity:
, where nis the number of elements in the input array,log(k)is the complexity of calculating the GCD of an element withk, andD(k)represents the number of distinct divisors ofk. This accounts for iterating through each element, calculating the GCD, and checking divisibility conditions with all entries in a map. -
Space Complexity:
, which is determined by the number of distinct GCD values stored in the map, corresponding to the number of distinct divisors of k. This space is used to track the frequency of each GCD value among the elements of the array.
On this page
Problem Statement
Examples
Solution
Step-by-Step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis